package com.waimai.demo.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.waimai.demo.Service.UserService;
import com.waimai.demo.back.R;
import com.waimai.demo.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("login")
    public R<User> login(@RequestBody User user, HttpServletRequest request) {
        log.info("登陆的号码是" + user.getPhone());
        User person = null;
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(user.getPhone() != null, User::getPhone, user.getPhone());
        User one = userService.getOne(lambdaQueryWrapper);
        if (one == null) {
            one = new User();
            one.setPhone(user.getPhone());
            one.setStatus(1);
            log.info("程序执行了哦===============");
            userService.save(one);

        }
        request.getSession().setAttribute("user", one.getId());
        return R.success(user);

    }
}
